package com.yidu.hbats.service.TrainingPlan;

import com.github.pagehelper.PageHelper;
import com.yidu.hbats.action.vo.AtsPlanPhasevo;
import com.yidu.hbats.dao.AtsPlanPhaseMapper;
import com.yidu.hbats.model.*;
import com.yidu.springbootdemo.permission.dao.TuserMapper;
import com.yidu.springbootdemo.permission.domain.Tuser;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 @类的描述——阶段计划
 @author 朱星润
 @date 2019/10/30下午 06:19
 */
@Service
public class AtsPlanPhaseServiceImpl implements AtsPlanPhaseService{
    @Resource
    AtsPlanPhaseMapper atsPlanPhaseMapper;
    //训练指标
    @Resource
    AtsPlanItemService atsPlanItemService;
    @Resource
    TuserMapper managerModelMapper;
    //运动员和计划的接口
    @Resource
    AtsEmpAthleteSchemeService atsEmpAthleteSchemeService;
    @Override
    public int count(HttpServletRequest request, AtsPlanPhase atsPlanPhase) {

        HttpSession session = request.getSession();
        Tuser manager = (Tuser) session.getAttribute("user");
        int count = 0;
        if(manager.getCompany()!=null && !"".equals(manager.getCompany())){
            if (manager.getCompanyNum()=="2"||"2".equals(manager.getCompanyNum())) {
                atsPlanPhase.setCoachName(manager.getCompanyName());
                atsPlanPhase.setCoachId(Integer.valueOf(manager.getFilId()));
                count = atsPlanPhaseMapper.countCoach(atsPlanPhase);
            } else if (manager.getCompanyNum()=="4"||"4".equals(manager.getCompanyNum())) {
                atsPlanPhase.setAthleteName(manager.getCompanyName());
                atsPlanPhase.setAthleteId(Integer.valueOf(manager.getFilId()));
                count = atsPlanPhaseMapper.countAthlete(atsPlanPhase);
            } else {
                count = atsPlanPhaseMapper.countAll(atsPlanPhase);
            }
        } else {
            count = atsPlanPhaseMapper.countAll(atsPlanPhase);
        }
        return count;
    }
    @Override
    public List<AtsPlanPhase> selectAll(HttpServletRequest request,AtsPlanPhase atsPlanPhase,Integer pageNum, Integer pageSize) {
        HttpSession session = request.getSession();
        Tuser manager = (Tuser) session.getAttribute("user");
        PageHelper.startPage(pageNum, pageSize);
        List<AtsPlanPhase> list = null;
        if(manager.getCompany()!=null && !"".equals(manager.getCompany())){
        if (manager.getCompanyNum()=="2"||"2".equals(manager.getCompanyNum())) {
            atsPlanPhase.setCoachName(manager.getCompanyName());
            atsPlanPhase.setCoachId(Integer.valueOf(manager.getFilId()));
            list = atsPlanPhaseMapper.findCoach(atsPlanPhase);
        } else if (manager.getCompanyNum()=="4"||"4".equals(manager.getCompanyNum())) {
            atsPlanPhase.setAthleteName(manager.getCompanyName());
            atsPlanPhase.setAthleteId(Integer.valueOf(manager.getFilId()));
            list = atsPlanPhaseMapper.findAthlete(atsPlanPhase);
        } else {
            list = atsPlanPhaseMapper.selectAll(atsPlanPhase);
        }
        } else {
            list = atsPlanPhaseMapper.selectAll(atsPlanPhase);
        }

        return list;
    }


    @Override
    public int updateIsva(AtsPlanPhase atsPlanPhase) {
        atsPlanPhase.setIsva(0);
        int rows=atsPlanPhaseMapper.updateIsva(atsPlanPhase);

        return rows;
    }

    @Override
    public int delAll(String id) {
        String[] ids = id.split(",");
        int rows=0;

        for(int i = 0;i<ids.length;i++){
            //删除计划同时删除下面指标
            AtsPlanItem atsPlanItem=new AtsPlanItem();
            atsPlanItem.setIsva(0);
            atsPlanItem.setPid(Integer.valueOf(ids[i]));
            //删除对应的运动员
            AtsEmpAthleteScheme atsEmpAthleteScheme=new AtsEmpAthleteScheme();
            atsEmpAthleteScheme.setIsva(0);
            atsEmpAthleteScheme.setPid(Integer.valueOf(ids[i]));

            AtsPlanPhase atsPlanPhase=new AtsPlanPhase();
            atsPlanPhase.setPhaseId(Integer.valueOf(ids[i]));
            atsPlanPhase.setIsva(0);
            rows=updateIsva(atsPlanPhase);
            atsEmpAthleteSchemeService.updatePidThree(atsEmpAthleteScheme);
            atsPlanItemService.updateOrdeletefour(atsPlanItem);
            rows++;
        }
        return rows;
    }



    @Override
    public AtsPlanPhase findMaxPid(int coachId) {
        return atsPlanPhaseMapper.findMaxPid(coachId);
    }

    @Override
    public List<AtsPlanPhase> selectAllTwo(String creater) {
        return atsPlanPhaseMapper.selectAllTwo(creater);
    }

    @Override
    public AtsPlanPhase selectByPrimaryKey(Integer phaseId) {
        return atsPlanPhaseMapper.selectByPrimaryKey(phaseId);
    }

    @Override
    public void insertSelective(AtsPlanPhasevo record) {
        String strDate=record.getYearTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime = null;
        try {
            dateTime = simpleDateFormat.parse(strDate);
            record.setPhaseDate(dateTime);
            AtsPlanPhase atsPlanPhase=new AtsPlanPhase();
            Date utilDate = new Date();//util.Date
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            record.setCreatTime(sqlDate);
            BeanUtils.copyProperties(record,atsPlanPhase);
            atsPlanPhaseMapper.insertSelective(atsPlanPhase);
            AtsPlanItem atsPlanItem=new AtsPlanItem();
            atsPlanItem.setRemark("3");
            atsPlanItem.setIsva(1);
            atsPlanItem.setCreatTime(sqlDate);
            atsPlanItem.setCreater(record.getCreater());
            //阶段计划计划传4 靠这个区别和查询
            atsPlanItem.setPlandate("4");
            atsPlanItem.setPid(atsPlanPhase.getPhaseId());
            atsPlanItem.setPname(atsPlanPhase.getPhaseName());
            boolean f=record.getItemNameone().contains(",");
            if(f==true){
                String[] arr= record.getItemNameone().split(",");
                String[] arrtwo= record.getItemValueone().split(",");
                for (int i = 0; i < arr.length; i++) {
                    String s = arr[i];
                    atsPlanItem.setItemName(s);
                    String sa = arrtwo[i];
                    atsPlanItem.setItemValue(sa);
                    atsPlanItemService.insertSelective(atsPlanItem);
                }

            }else{
                atsPlanItem.setItemName(record.getItemNameone());
                atsPlanItem.setItemValue(record.getItemValueone());
                atsPlanItemService.insertSelective(atsPlanItem);
            }
            //new出计划教练员关联表
            AtsEmpAthleteScheme atsEmpAthlete=new AtsEmpAthleteScheme();
            atsEmpAthlete.setCreater(record.getCreater());
            //传入3代表阶段训练计划
            atsEmpAthlete.setRemark("3");
            atsEmpAthlete.setPname(record.getPhaseName());
            atsEmpAthlete.setPid(atsPlanPhase.getPhaseId());
            boolean flag=record.getAthleteId().contains(",");
            if(flag==true) {
                String[] athleteId = record.getAthleteId().split(",");
                String[] athleteName = record.getAthleteName().split(",");
                for (int i = 0; i < athleteId.length; i++) {
                    String athleteIdone = athleteId[i];
                    String athleteNameone = athleteName[i];
                    atsEmpAthlete.setAthleteId(Integer.valueOf(athleteIdone));
                    atsEmpAthlete.setAthleteName(athleteNameone);
                    atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
                }
            }else{
                atsEmpAthlete.setAthleteId(Integer.valueOf(record.getAthleteId()));
                atsEmpAthlete.setAthleteName(record.getAthleteName());
                atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void updateByPrimaryKeySelective(AtsPlanPhasevo record) {
        String strDate=record.getYearTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime = null;
        try {
            dateTime = simpleDateFormat.parse(strDate);
            record.setPhaseDate(dateTime);
            AtsPlanPhase atsPlanPhase=new AtsPlanPhase();
            atsPlanPhase.setSportId(record.getSportId());
            Date utilDate = new Date();//util.Date
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            record.setCreatTime(sqlDate);
            BeanUtils.copyProperties(record,atsPlanPhase);
            atsPlanPhaseMapper.updateIsva(atsPlanPhase);
            AtsPlanItem atsPlanItemt=new AtsPlanItem();
            atsPlanItemt.setIsva(0);
            atsPlanItemt.setPid(record.getPhaseId());
            AtsEmpAthleteScheme atsEmpAthleteScheme=new AtsEmpAthleteScheme();
            atsEmpAthleteScheme.setIsva(0);
            atsEmpAthleteScheme.setPid(record.getPhaseId());
            int rowsone=atsEmpAthleteSchemeService.updatePidThree(atsEmpAthleteScheme);
            if(rowsone>0){
                //new出计划教练员关联表
                AtsEmpAthleteScheme atsEmpAthlete=new AtsEmpAthleteScheme();
                atsEmpAthlete.setCreater(record.getCreater());
                //传入3代表阶段训练计划
                atsEmpAthlete.setRemark("3");
                atsEmpAthlete.setPname(record.getPhaseName());
                atsEmpAthlete.setPid(atsPlanPhase.getPhaseId());
                boolean flag=record.getAthleteId().contains(",");
                if(flag==true) {
                    String[] athleteId = record.getAthleteId().split(",");
                    String[] athleteName = record.getAthleteName().split(",");
                    for (int i = 0; i < athleteId.length; i++) {
                        String athleteIdone = athleteId[i];
                        String athleteNameone = athleteName[i];
                        atsEmpAthlete.setAthleteId(Integer.valueOf(athleteIdone));
                        atsEmpAthlete.setAthleteName(athleteNameone);
                        atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
                    }
                }else{
                    atsEmpAthlete.setAthleteId(Integer.valueOf(record.getAthleteId()));
                    atsEmpAthlete.setAthleteName(record.getAthleteName());
                    atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
                }
            }
            int rows=atsPlanItemService.updateOrdeletefour(atsPlanItemt);
            if(rows>0){
                AtsPlanItem atsPlanItem=new AtsPlanItem();
                atsPlanItem.setRemark("3");
                atsPlanItem.setIsva(1);
                atsPlanItem.setCreatTime(sqlDate);
                atsPlanItem.setCreater(record.getCreater());
                //阶段计划计划传4 靠这个区别和查询
                atsPlanItem.setPlandate("4");
                atsPlanItem.setPid(atsPlanPhase.getPhaseId());
                atsPlanItem.setPname(atsPlanPhase.getPhaseName());
                boolean f=record.getItemNameone().contains(",");
                if(f==true){
                    String[] arr= record.getItemNameone().split(",");
                    String[] arrtwo= record.getItemValueone().split(",");
                    for (int i = 0; i < arr.length; i++) {
                        String s = arr[i];
                        atsPlanItem.setItemName(s);
                        String sa = arrtwo[i];
                        atsPlanItem.setItemValue(sa);
                        atsPlanItemService.insertSelective(atsPlanItem);
                    }

                }else{
                    atsPlanItem.setItemName(record.getItemNameone());
                    atsPlanItem.setItemValue(record.getItemValueone());
                    atsPlanItemService.insertSelective(atsPlanItem);
                }
            }else{
                AtsPlanItem atsPlanItem=new AtsPlanItem();
                atsPlanItem.setRemark("3");
                atsPlanItem.setIsva(1);
                atsPlanItem.setCreatTime(sqlDate);
                atsPlanItem.setCreater(record.getCreater());
                //阶段计划计划传4 靠这个区别和查询
                atsPlanItem.setPlandate("4");
                atsPlanItem.setPid(atsPlanPhase.getPhaseId());
                atsPlanItem.setPname(atsPlanPhase.getPhaseName());
                boolean f=record.getItemNameone().contains(",");
                if(f==true){
                    String[] arr= record.getItemNameone().split(",");
                    String[] arrtwo= record.getItemValueone().split(",");
                    for (int i = 0; i < arr.length; i++) {
                        String s = arr[i];
                        atsPlanItem.setItemName(s);
                        String sa = arrtwo[i];
                        atsPlanItem.setItemValue(sa);
                        atsPlanItemService.insertSelective(atsPlanItem);
                    }

                }else{
                    atsPlanItem.setItemName(record.getItemNameone());
                    atsPlanItem.setItemValue(record.getItemValueone());
                    atsPlanItemService.insertSelective(atsPlanItem);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<AtsPlanPhase> selectAll(AtsPlanPhase atsPlanPhase) {
        return atsPlanPhaseMapper.selectAll(atsPlanPhase);
    }
}
